﻿Public Class FormTipoMaterialListado
    

    Dim classController As New Class_RC_Controller.rc_Mantenimiento()

    Private m_frmtipomaterial As FrmTipomate
    Public Property frmtipomaterial() As FrmTipomate
        Get
            Return m_frmtipomaterial
        End Get
        Set(ByVal value As FrmTipomate)
            m_frmtipomaterial = value
        End Set
    End Property

    Private Sub Cargartipomaterial()
        Dim CodUnidadMedida As Integer
        If cbxunidad.Checked Then
            CodUnidadMedida = cmbunidad.SelectedValue
        End If
        Dim dt As New DataTable
        dt = classController.get_TipoMaterial_All(oiml.dbConn, 0, txtDescripcion.Text, CodUnidadMedida)
        dgTipoMaterial.DataSource = dt
    End Sub
    Private Sub llenarunidad()
        Dim dsunidad As New DataTable
        dsunidad = classController.get_tipo_unidMedida(oiml.dbConn)
        If Not dsunidad Is Nothing Then
            If dsunidad.Rows.Count > 0 Then
                cmbunidad.DataSource = dsunidad
                cmbunidad.DisplayMember = "descUnidadMedida"
                cmbunidad.ValueMember = "codUnidadMedida"
            End If
        End If
    End Sub

    Private Sub txtDescripcion_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescripcion.KeyDown
        If e.KeyCode = Keys.Enter Then
            Cargartipomaterial()
        End If
    End Sub
    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgTipoMaterial.SelectedRows.Count > 0 Then
                Dim Codtipomaterial As New Integer
                Codtipomaterial = dgTipoMaterial.SelectedRows(0).Cells(0).Value

                If frmtipomaterial Is Nothing Then
                    frmtipomaterial = New FrmTipomate
                    frmtipomaterial.codtipomaterial = Codtipomaterial
                    'If frmServicios.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmtipomaterial.ShowDialog()
                    Cargartipomaterial()
                    frmtipomaterial.Dispose()
                    frmtipomaterial = Nothing
                    'End If
                Else
                    frmtipomaterial.codtipomaterial = Codtipomaterial
                    frmtipomaterial.ShowDialog()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub
    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        Cargartipomaterial()
    End Sub

    Private Sub FormTipoMaterialListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgTipoMaterial.AutoGenerateColumns = False
        llenarunidad()
        Cargartipomaterial()

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        '    If frmciudad Is Nothing Then
        frmtipomaterial = New FrmTipomate()
        frmtipomaterial.ShowDialog()
        Cargartipomaterial()

    End Sub

    
    Private Sub dgTipoMaterial_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgTipoMaterial.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgTipoMaterial_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgTipoMaterial.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If
    End Sub
End Class